package com.zyy.smartcity1129.movie;

import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.appcompat.widget.SearchView;
import androidx.lifecycle.MutableLiveData;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.google.android.material.tabs.TabLayout;
import com.youth.banner.Banner;
import com.youth.banner.indicator.CircleIndicator;
import com.zyy.smartcity1129.BaseFragment;
import com.zyy.smartcity1129.R;
import com.zyy.smartcity1129.data.MovieCategory;
import com.zyy.smartcity1129.data.MovieNews;
import com.zyy.smartcity1129.data.ad;
import com.zyy.smartcity1129.util.BannerAd;
import com.zyy.smartcity1129.util.Http;
import com.zyy.smartcity1129.util.RvAd;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @ClassName: MovieMain$
 * @Description: 作用
 * @Author: letquit
 * @CreateDate: 17/11/2021$
 * @Version: 1.0
 */
public class MovieMainFragment extends BaseFragment {
    private Banner banner;
    private SearchView search;
    private TabLayout tab;
    private RecyclerView rv;
    ExecutorService execut= Executors.newFixedThreadPool(5);
    MutableLiveData<AtomicInteger> mData = new MutableLiveData<>();
    public AtomicInteger mInteger = new AtomicInteger(0);
    private List<ad.RowsBean> adList = new ArrayList<>();
    private List<MovieCategory.DataBean> categoryList;
    private List<MovieNews.RowsBean> newsList;
    public static int newsId;
    public static String names;
    @Override
    protected int layId() {
        return R.layout.fragment_movie_main;
    }

    @Override
    protected String title() {
        return null;
    }

    @Override
    protected boolean back() {
        return false;
    }

    @Override
    protected Boolean bottom() {
        return false;
    }

    @Override
    protected void init(View v) {
        MovieFragment.mutableLiveData.postValue("首页");
        initView(v);
//        广告
        execut.execute(new Runnable() {
            @Override
            public void run() {
                adList = Http.getInstance().get("/prod-api/api/movie/rotation/list", null, ad.class, 1).getRows();
                mInteger.getAndIncrement();
                mData.postValue(mInteger);
            }
        });
//        新闻类别
        execut.execute(new Runnable() {
            @Override
            public void run() {
                categoryList = Http.getInstance().get("/prod-api/api/movie/press/category/list", null, MovieCategory.class, 1).getData();
                mInteger.getAndIncrement();
                mData.postValue(mInteger);
            }
        });
//        新闻详细
       execut.execute(new Runnable() {
           @Override
           public void run() {
               newsList = Http.getInstance().get("/prod-api/api/movie/press/press/list", null, MovieNews.class, 1).getRows();
               mInteger.getAndIncrement();
               mData.postValue(mInteger);
           }
       });
        mData.observe(this, atomicInteger -> {
            if (atomicInteger.get() == 3) {
//            banner
                List<Object> imgList = new ArrayList<>();
                for (ad.RowsBean ad : adList) {
                    imgList.add(ad.getAdvImg());
                }
                banner.setAdapter(new BannerAd(imgList)).setIndicator(new CircleIndicator(getContext()))
                        .addBannerLifecycleObserver(this).start();

//           news
                RvAd<MovieNews.RowsBean> news = new RvAd<MovieNews.RowsBean>(newsList, R.layout.rv_ad) {
                    private LinearLayout rv_line;
                    private LinearLayout rv_line1;
                    private LinearLayout rv_line2;
                    private TextView rv_tv1;
                    private TextView rv_tv2;
                    private TextView rv_tv3;
                    private TextView rv_tv4;
                    private TextView rv_tv5;
                    private TextView rv_tv6;
                    private TextView rv_tv7;
                    private TextView rv_tv11;
                    private ImageView rv_ig;

                    private void initView(View v) {
                        rv_line = (LinearLayout) v.findViewById(R.id.rv_line);
                        rv_line1 = (LinearLayout) v.findViewById(R.id.rv_line1);
                        rv_line2 = (LinearLayout) v.findViewById(R.id.rv_line2);
                        rv_ig = (ImageView) v.findViewById(R.id.rv_ig);
                        rv_tv1 = (TextView) v.findViewById(R.id.rv_tv1);
                        rv_tv2 = (TextView) v.findViewById(R.id.rv_tv2);
                        rv_tv3 = (TextView) v.findViewById(R.id.rv_tv3);
                        rv_tv4 = (TextView) v.findViewById(R.id.rv_tv4);
                        rv_tv5 = (TextView) v.findViewById(R.id.rv_tv5);
                        rv_tv6 = (TextView) v.findViewById(R.id.rv_tv6);
                        rv_tv7 = (TextView) v.findViewById(R.id.rv_tv7);
                        rv_tv11 = (TextView) v.findViewById(R.id.rv_tv11);
                    }
                    @Override
                    protected void initR(View iv, MovieNews.RowsBean rowsBean, int p) {
                        initView(iv);
                        rv_line.setVisibility(View.VISIBLE);
                        rv_line2.setVisibility(View.GONE);
                        rv_tv11.setVisibility(View.GONE);
                        Glide.with(rv_ig.getContext()).load(Http.base + rowsBean.getCover()).into(rv_ig);
//                    内容
                        rv_tv1.setText(rowsBean.getTitle());
                        String result;
                        List<String> list = getChildMsgByParent(rowsBean.getContent(), "<p>导演:", "<p>编剧:");
                        for (String s : list) {
                            result = s.substring(3, s.length() - 10);
                            rv_tv2.setText(result);
                            break;
                        }
                        String result1;
                        List<String> list1 = getChildMsgByParent(rowsBean.getContent(), "<p>主演:", "<p>类型:");
                        for (String s : list1) {
                            result1 = s.substring(3, s.length() - 10).replace(" ", "");
                            rv_tv3.setText(result1);
                            break;
                        }
                        rv_tv6.setText(String.valueOf(rowsBean.getCommentNum()));
                        rv_tv7.setText("上映时间：" + rowsBean.getPublishDate());
                        rv_line.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                newsId = rowsBean.getId();
                                names = rowsBean.getTitle();
                                add(new MovieNewsFragment());
                            }
                        });
                    }
                };
                rv.setLayoutManager(new LinearLayoutManager(getContext()));
                rv.setAdapter(news);

//           category
                List<String> title = new ArrayList<>();
                for (MovieCategory.DataBean m : categoryList) {
                    title.add(m.getName());
                }
                tab.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
                    @Override
                    public void onTabSelected(TabLayout.Tab tab) {
                    }

                    @Override
                    public void onTabUnselected(TabLayout.Tab tab) {

                    }

                    @Override
                    public void onTabReselected(TabLayout.Tab tab) {

                    }
                });
                tab.removeAllTabs();
                tab.setTabMode(TabLayout.MODE_FIXED);
                for (String s : title) {
                    tab.addTab(tab.newTab().setText(s));
                }
                tab.selectTab(tab.getTabAt(0));
            }
//            categories.addAll(categoryList.stream().filter(a -> a.getName().equals(s.get(0))).collect(Collectors.toList()));
        });
    }

    private void initView(View v) {
        banner = (Banner) v.findViewById(R.id.bannerMovieMain);
        search = (SearchView) v.findViewById(R.id.searchMovieMain);
        tab = (TabLayout) v.findViewById(R.id.tabMovieMain);
        rv = (RecyclerView) v.findViewById(R.id.rvMovieMain);
    }

    public static List<String> getChildMsgByParent(String info,String begin,String end){
        //通过起始字符拆分成数组
        String[] split = info.split(begin);
        List<String> list = new ArrayList<>();
        //遍历，从第二个元素开始取值（第一个元素为无效元素）
        for (int i = 1; i < split.length; i++) {
            String str = split[i].substring(0,split[i].lastIndexOf(end)+end.length());
            if (str.length() > end.length()) {
                list.add(begin+str);
            }
        }
        return list;
    }

}
